import moduleCreator from '../moduleCreator'

import api from '@/api'

import { get_image } from '@/api/utils'

const modelName = 'mail.channel'

const state = {}

const mutations = {}

const actions = {
  async search_me({ commit, state, dispatch }, payload) {
    const { partner_id } = await api.get_userinfo()
    // console.log('me getInfo', uid, partner_id)
    const Ptn = api.env('res.partner')

    const partner = await Ptn.browse_one(partner_id, {
      fields: {
        private_channel_ids: {
          opp_partner_id: null,
          opp_partner_name: null
        }
      }
    })
    // console.log('xxxx, getInfo', partner)

    const chns = partner.private_channel_ids__objects
    const chns2 = chns.map(item => {
      return {
        ...item,
        name: item.opp_partner_name,
        image: get_image('res.partner', item.opp_partner_id, 'image_1920')
      }
    })

    commit('SET_DataList', chns2)
    return new Promise(resolve => {
      resolve(chns2)
    })
  }
}

export default moduleCreator(modelName, {
  namespaced: true,
  state,
  mutations,
  actions
})
